package com.david.trade.api.event;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 */
public class TcpEventSource {
    private List<TcpEventListener> listeners;

    public TcpEventSource() {
        this.listeners = new ArrayList<>();
    }

    public void register(TcpEventListener listener) {    // 1
        listeners.add(listener);
    }

    public void newEvent(TcpEvent event) {
        for (TcpEventListener listener : listeners) {
            listener.onNewEvent(event);
        }
    }

    public void eventStopped() {
        for (TcpEventListener listener :
                listeners) {
            listener.onEventStopped();
        }
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TcpEvent {
        private Date timeStamp;
        private String message;
        private String type;
    }
}
